#include "stdafx.h"
#include "geom.h"
#include "data.h"
#include <cassert>

void serialize_geometry(shared_ptr<Geometry> data, ostream& out)
{
	out.write((char*)&data->m_vertices_count, sizeof(decltype(data->m_vertices_count)));
	out.write((char*)&data->m_vertex_stride, sizeof(decltype(data->m_vertex_stride)));
	out.write((char*)&data->m_indexes_count, sizeof(decltype(data->m_indexes_count)));

	out.write(data->vertices_pointer(), data->vertices_byte_size());
	out.write((char*)data->indexes_pointer(), data->indexes_byte_size());
}

shared_ptr<Geometry> deserialize_geometry(istream& in)
{
	uint vertices_count = 0, vertex_stride = 0, indexes_count = 0;

	in.read((char*)&vertices_count, sizeof(decltype(vertices_count)));
	in.read((char*)&vertex_stride, sizeof(decltype(vertex_stride)));
	in.read((char*)&indexes_count, sizeof(decltype(indexes_count)));

	shared_ptr<Geometry> data(new Geometry(vertices_count, vertex_stride, indexes_count));

	in.read(data->vertices_pointer(), data->vertices_byte_size());
	in.read((char*)data->indexes_pointer(), data->indexes_byte_size());

	return data;
}
shared_ptr<Geometry> fullscreen_quad()
{
	shared_ptr<Geometry> data(new Geometry(4, sizeof(StaticVertex), 6));

	StaticVertex* vp = (StaticVertex*)data->vertices_pointer();
	vp[0].position = float3(-1, -1, 0);
	vp[1].position = float3(1, -1, 0);
	vp[2].position = float3(1, 1, 0);
	vp[3].position = float3(-1, 1, 0);


	Index* ip = data->indexes_pointer();
	ip[0] = 2;
	ip[1] = 1;
	ip[2] = 0;

	ip[3] = 0;
	ip[4] = 3;
	ip[5] = 2;
	return data;
}

shared_ptr<Geometry> animated_triangle()
{
	shared_ptr<Geometry> data(new Geometry(3, sizeof(AnimatedVertex), 3));

	auto vp = (AnimatedVertex*)data->vertices_pointer();
	vp[0].position = float3(-1, -1, 0);
	vp[1].position = float3(1, -1, 0);
	vp[2].position = float3(0, 1, 0);

	vp[0].uv = float2(0,0);
	vp[1].uv = float2(1, 0);
	vp[2].uv = float2(0, 1);

	vp[0].normal = float3(0,0,1);
	vp[1].normal = float3(0,0,1);
	vp[2].normal = float3(0,0,1);

	vp[0].bones[0] = 0;
	vp[0].bone_weights[0] = .5;
	vp[0].bones[1] = 1;
	vp[0].bone_weights[1] = .5;


	vp[1].bones[0] = 0;
	vp[1].bone_weights[0] = .5;
	vp[1].bones[1] = 1;
	vp[1].bone_weights[1] = .5;


	vp[2].bones[0] = 0;
	vp[2].bone_weights[0] = .5;
	vp[2].bones[1] = 1;
	vp[2].bone_weights[1] = .5;



	uint* ip = data->indexes_pointer();
	ip[0] = 2;
	ip[1] = 1;
	ip[2] = 0;
	return data;
}
shared_ptr<Geometry> triangle()
{
	shared_ptr<Geometry> data(new Geometry(3, sizeof(StaticVertex), 3));
	auto vp = (StaticVertex*)data->vertices_pointer();
	vp[0].position = float3(-1, -1, 0);
	vp[1].position = float3(1, -1, 0);
	vp[2].position = float3(0, 1, 0);

	vp[0].uv = float2(0,0);
	vp[1].uv = float2(1, 0);
	vp[2].uv = float2(0, 1);

	vp[0].normal = float3(0,0,1);
	vp[1].normal = float3(0,0,1);
	vp[2].normal = float3(0,0,1);

	uint* ip = data->indexes_pointer();
	ip[0] = 2;
	ip[1] = 1;
	ip[2] = 0;
	return data;
}